home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / gnu / gmp_1_3_.gz / gmp_1_3_ / gmp-1.3.2 / tests / tst-gcd.c < prev    next >
C/C++ Source or Header  |  1993-05-06  |  3KB  |  132 lines

  1. /* Test mpz_gcd, mpz_gcdext, mpz_mul, mpz_mod, mpz_add, mpz_cmp,
  2.    mpz_cmp_ui. mpz_init_set, mpz_set, mpz_clear.
  3.  
  4. Copyright (C) 1991, 1993 Free Software Foundation, Inc.
  5.  
  6. This file is part of the GNU MP Library.
  7.  
  8. The GNU MP Library is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. The GNU MP Library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with the GNU MP Library; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <stdio.h>
  23. #include "gmp.h"
  24. #include "gmp-impl.h"
  25. #include "urandom.h"
  26.  
  27. void mpz_refgcd (), debug_mp ();
  28.  
  29. #ifndef SIZE
  30. #define SIZE 8
  31. #endif
  32.  
  33. main (argc, argv)
  34.      int argc;
  35.      char **argv;
  36. {
  37.   MP_INT op1, op2;
  38.   MP_INT refgcd, gcd, s, t;
  39.   MP_INT temp1, temp2;
  40.   mp_size op1_size, op2_size;
  41.   int i;
  42.   int reps = 10000;
  43.  
  44.   if (argc == 2)
  45.      reps = atoi (argv[1]);
  46.  
  47.   mpz_init (&op1);
  48.   mpz_init (&op2);
  49.   mpz_init (&refgcd);
  50.   mpz_init (&gcd);
  51.   mpz_init (&temp1);
  52.   mpz_init (&temp2);
  53.   mpz_init (&s);
  54.   mpz_init (&t);
  55.  
  56.   for (i = 0; i < reps; i++)
  57.     {
  58.       op1_size = urandom () % SIZE;
  59.       op2_size = urandom () % SIZE;
  60.  
  61.       mpz_random2 (&op1, op1_size);
  62.       mpz_random2 (&op2, op2_size);
  63.  
  64.       mpz_refgcd (&refgcd, &op1, &op2);
  65.  
  66.       mpz_gcd (&gcd, &op1, &op2);
  67.       if (mpz_cmp (&refgcd, &gcd))
  68.     dump_abort (&op1, &op2);
  69.  
  70.       mpz_gcdext (&gcd, &s, &t, &op1, &op2);
  71.       if (mpz_cmp (&refgcd, &gcd))
  72.     dump_abort (&op1, &op2);
  73.  
  74.       mpz_mul (&temp1, &s, &op1);
  75.       mpz_mul (&temp2, &t, &op2);
  76.       mpz_add (&gcd, &temp1, &temp2);
  77.       if (mpz_cmp (&refgcd, &gcd))
  78.     dump_abort (&op1, &op2);
  79.     }
  80.  
  81.   exit (0);
  82. }
  83.  
  84. void
  85. mpz_refgcd (g, x, y)
  86.      MP_INT *g;
  87.      MP_INT *x, *y;
  88. {
  89.   MP_INT xx, yy;
  90.  
  91.   mpz_init (&xx);
  92.   mpz_init (&yy);
  93.  
  94.   mpz_abs (&xx, x);
  95.   mpz_abs (&yy, y);
  96.  
  97.   for (;;)
  98.     {
  99.       if (mpz_cmp_ui (&yy, 0) == 0)
  100.     {
  101.       mpz_set (g, &xx);
  102.       break;
  103.     }
  104.       mpz_mod (&xx, &xx, &yy);
  105.       if (mpz_cmp_ui (&xx, 0) == 0)
  106.     {
  107.       mpz_set (g, &yy);
  108.       break;
  109.     }
  110.       mpz_mod (&yy, &yy, &xx);
  111.     }
  112.  
  113.   mpz_clear (&xx);
  114.   mpz_clear (&yy);
  115. }
  116.  
  117. dump_abort (op1, op2)
  118.      MP_INT *op1, *op2;
  119. {
  120.   fprintf (stderr, "ERROR\n");
  121.   fprintf (stderr, "op1 = "); debug_mp (op1, -16);
  122.   fprintf (stderr, "op2 = "); debug_mp (op2, -16);
  123.   abort();
  124. }
  125.  
  126. void
  127. debug_mp (x, base)
  128.      MP_INT *x;
  129. {
  130.   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
  131. }
  132.